package com.example.transitionsdemo

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.readystatesoftware.systembartint.SystemBarTintManager

abstract class BaseActivity: AppCompatActivity() {

    protected abstract val layoutId: Int
    protected abstract fun initView()
    protected abstract fun initData()
    protected abstract fun initListener()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId)
    }

    protected open fun setImmerseLayout(view: View) { //设置沉浸式（4.4以上）
        setImmerseLayout()
        val statusBarHeight = getStatusBarHeight(this.baseContext)
        view.setPadding(0, statusBarHeight, 0, 0)
    }

    protected open fun setImmerseLayout() { //设置沉浸式（4.4以上）
        val window = window
        window.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.statusBarColor = Color.TRANSPARENT
    }

    /**
     * 设置状态栏的颜色，目前只是在4.4以上有效
     */
    open fun setStatusBarView(color: Int) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        val tintManager = SystemBarTintManager(this)
        tintManager.isStatusBarTintEnabled = true
        tintManager.setNavigationBarTintEnabled(false)
        tintManager.setTintColor(color)
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    open fun getStatusBarHeight(context: Context): Int {
        var result = 0
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }
}